package domotica.house.simulation.utils;

/**
 * Classe per la simulazione di una variabile fisica.
 */
public class PhysicVariableSimulator implements Runnable{
	 
	private double minValue;
	private double maxValue;
	private double variationForSecond;
	private double variable;

	public PhysicVariableSimulator(double min, double max, double startValue, double variationForSecond){
		this.minValue=min;
		this.maxValue=max;
		this.variable=startValue;
		this.variationForSecond=variationForSecond;
	}

	public void run() {
		while(true){
			if (Math.random()>0.5&&(this.getPhysicVariable()+variationForSecond<maxValue)){
				this.changePhysicVariable(this.variationForSecond);
			} else {
				if (this.getPhysicVariable()-variationForSecond>minValue)
					this.changePhysicVariable(-this.variationForSecond);
			}
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public synchronized double changePhysicVariable(double variation){
		return this.variable+=variation;
	}

	public synchronized double getPhysicVariable(){
		return this.variable;
	}
	
}
